<template>
  <el-dialog
    title="关联权限"
    :visible.sync="showDialog"
    width="45%"
  >
    <!-- 绘制树 -->
    <el-tree
      v-if="showDialog"
      ref="tree"
      default-expand-all
      :data="allAuthList"
      :props="{label: 'name'}"
      show-checkbox
      node-key="id"
      :default-checked-keys="checkAuth"
      :check-strictly="true"
    />
    <span />
    <div slot="footer">
      <el-button size="small" @click="showDialog = false">取 消</el-button>
      <el-button size="small" type="primary" @click="btnOK">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import { getDepartmentListApi } from '@/api/department'
import { transListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
import { setRoleAuthApi } from '@/api/role'
// import { setRoleAuthApi } from '@/api/role'
export default {
  data() {
    return {
      showDialog: false,
      allAuthList: [], // 所有权限
      checkAuth: [], // 默认选中权限
      roleId: ''
    }
  },
  created() {
    this.getAllAuth()
  },
  methods: {
    // 获取所有权限
    async getAllAuth() {
      const res = await getPermissionList()
      console.log(res)
      this.allAuthList = transListToTreeData(res, 0)
    },
    // 角色关联权限提交
    async btnOK() {
      console.log(this.$refs.tree.getCheckedKeys())
      await setRoleAuthApi({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('操作成功')
      this.showDialog = false
      this.checkAuth = []
    }
  }

}
</script>

<style>

</style>
